
/*
 This file is part of BTStracker
		
    BTStracker is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    BTStracker is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Foobar.  If not, see <http://www.gnu.org/licenses/>.	
	author: azbest.pro (azbest.pro@gmail.com)
*/
package pl.vdl.azbest.mremote.mob;

import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import java.util.logging.Logger;

import pl.vdl.azbest.log.LoggingTheGathering;
import pl.vdl.azbest.mremote.Conf;

import sun.text.normalizer.UProperty;

public class ATOutputStream extends OutputStream {
	private String anwser = "";

	private List<ATObserver> observers = new ArrayList<ATObserver>();

	private String at;

	private static ATOutputStream instance = new ATOutputStream();

	private Logger logger = Logger.getLogger(getClass().getName());
	{
		LoggingTheGathering.addPath(getClass().getName());

	}

	public static ATOutputStream getInstance() {
		if (instance == null) {
			instance = new ATOutputStream();
			return instance;
		} else {
			return instance;
		}
	}

	private ATOutputStream() {
	}

	public void write(int b) throws IOException {
		char r = (char) b;
		anwser += r;
		// System.out.println(">" + anwser + "___");
		if (anwser.endsWith("OK" + System.getProperty("line.separator"))) {
			at = anwser;
			notifyObservers();
			anwser = "";
			// System.out.println("End of anwser");
		}
	}

	public void addObservers(ATObserver listener) {
		this.observers.add(listener);
	}

	public void removeObservers(ATObserver listener) {
		this.observers.remove(listener);

	}

	public void notifyObservers() {
		StringTokenizer tokenizer = new StringTokenizer(at,
				Conf.getInstance().LineS);
		ATAnwser at = null;

		while (tokenizer.hasMoreElements()) {
			String element = (String) tokenizer.nextElement();
			logger.info(">" + element);
			element = element.trim();
			if (element.equals(Conf.getInstance().LineS)
					|| element.contains("OK") || element.contains("?")) {
				continue;
			} else if (element.contains("+CGREG: ")) {
				// +CGREG: 2,5,"0F2B","0000557B"
				String splited[] = element.split(",");
				at = ATAnwser.CGREG;
				at.setData(splited[2].replace("\"", ""), splited[3].replace(
						"\"", ""));
				logger.info(">>" + at.getData().get(0));
				logger.info(">>" + at.getData().get(1));
			} else if (element.contains("+CSQ:")) {

				// +CSQ: 18,99
				logger.info("we are in csq check");
				String splited[] = element.split(":");
				at = ATAnwser.CSQ;
				at.setData(splited[1].trim());
				logger.info("+++"+splited[1].trim());
				logger.info(">7>" + at.getData().get(0));
				splited = null;
				element = null;
			} else {
				at = ATAnwser.AT;
			}
		}// tokenizer

		for (ATObserver mat : observers) {

			mat.update(at);

		}
	}
}
